//
//  ModelOrderDetail.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/17.
//

#import "ModelOrderDetail.h"

NSString *const kModelOrderDetailOrderNo = KEY_orderNo;
NSString *const kModelOrderDetailProductName = KEY_productName;
NSString *const kModelOrderDetailSlogan = KEY_slogan;
NSString *const kModelOrderDetailStatus = KEY_status;
NSString *const kModelOrderDetailLoanAmount = KEY_loanAmount;
NSString *const kModelOrderDetailTermUnit = KEY_termUnit;
NSString *const kModelOrderDetailExpiryTime = KEY_expiryTime;
NSString *const kModelOrderDetailPairs = KEY_pairs;
NSString *const kModelOrderDetailTerm = KEY_term;
NSString *const kModelOrderDetailDelayStatus = KEY_delayStatus;
NSString *const kModelOrderDetailShowAutoReloanSwitch = KEY_showAutoReloanSwitch;
NSString *const kModelOrderDetailSettledAutoApplyAmount = KEY_settledAutoApplyAmount;

@interface ModelOrderDetail ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ModelOrderDetail


@synthesize slogan = _slogan;
@synthesize productName = _productName;
@synthesize loanAmount = _loanAmount;
@synthesize orderNo = _orderNo;
@synthesize termUnit = _termUnit;
@synthesize pairs = _pairs;
@synthesize term = _term;
@synthesize status = _status;
@synthesize expiryTime = _expiryTime;
@synthesize delayStatus = _delayStatus;
@synthesize showAutoReloanSwitch = _showAutoReloanSwitch;
@synthesize settledAutoApplyAmount = _settledAutoApplyAmount;

+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
        self.orderNo = [self objectOrNilForKey:kModelOrderDetailOrderNo fromDictionary:dict];
        self.productName = [self objectOrNilForKey:kModelOrderDetailProductName fromDictionary:dict];
        self.slogan = [self objectOrNilForKey:kModelOrderDetailSlogan fromDictionary:dict];
        self.expiryTime = [self objectOrNilForKey:kModelOrderDetailExpiryTime fromDictionary:dict];
        self.status = [self objectOrNilForKey:kModelOrderDetailStatus fromDictionary:dict];
        self.loanAmount = [self objectOrNilForKey:kModelOrderDetailLoanAmount fromDictionary:dict];
        self.termUnit = [[self objectOrNilForKey:kModelOrderDetailTermUnit fromDictionary:dict] doubleValue];
        self.term = [[self objectOrNilForKey:kModelOrderDetailTerm fromDictionary:dict] doubleValue];
        self.delayStatus = [[self objectOrNilForKey:kModelOrderDetailDelayStatus fromDictionary:dict] doubleValue];
        // pairs
        NSObject *receivedPairs = [dict objectForKey:kModelOrderDetailPairs];
        NSMutableArray *parsedPairs = [NSMutableArray array];
        if ([receivedPairs isKindOfClass:[NSArray class]]) {
            for (NSDictionary *item in (NSArray *)receivedPairs) {
                if ([item isKindOfClass:[NSDictionary class]]) {
                    [parsedPairs addObject:[ModelPairs modelObjectWithDictionary:item]];
                }
           }
        } else if ([receivedPairs isKindOfClass:[NSDictionary class]]) {
           [parsedPairs addObject:[ModelPairs modelObjectWithDictionary:(NSDictionary *)receivedPairs]];
        }
        self.pairs = [NSArray arrayWithArray:parsedPairs];
        self.showAutoReloanSwitch = [[self objectOrNilForKey:kModelOrderDetailShowAutoReloanSwitch fromDictionary:dict] boolValue];
        self.settledAutoApplyAmount = [[self objectOrNilForKey:kModelOrderDetailSettledAutoApplyAmount fromDictionary:dict] doubleValue];
    }
    return self;
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.orderNo forKey:kModelOrderDetailOrderNo];
    [mutableDict setValue:self.productName forKey:kModelOrderDetailProductName];
    [mutableDict setValue:self.slogan forKey:kModelOrderDetailSlogan];
    [mutableDict setValue:self.expiryTime forKey:kModelOrderDetailExpiryTime];
    [mutableDict setValue:self.status forKey:kModelOrderDetailStatus];
    [mutableDict setValue:self.loanAmount forKey:kModelOrderDetailLoanAmount];
    [mutableDict setValue:[NSNumber numberWithDouble:self.termUnit] forKey:kModelOrderDetailTermUnit];
    [mutableDict setValue:[NSNumber numberWithDouble:self.term] forKey:kModelOrderDetailTerm];
    [mutableDict setValue:[NSNumber numberWithDouble:self.delayStatus] forKey:kModelOrderDetailDelayStatus];
    // pairs
    NSMutableArray *tempArrayForPairs = [NSMutableArray array];
    for (NSObject *subArrayObject in self.pairs) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForPairs addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForPairs addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForPairs] forKey:kModelOrderDetailPairs];
    [mutableDict setValue:[NSNumber numberWithBool:self.showAutoReloanSwitch] forKey:kModelOrderDetailShowAutoReloanSwitch];
    [mutableDict setValue:[NSNumber numberWithDouble:self.settledAutoApplyAmount] forKey:kModelOrderDetailSettledAutoApplyAmount];
    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}

@end
